package com.cuxiaoyi.action;

import java.sql.Connection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.cuxiaoyi.Constants;
import com.cuxiaoyi.db.DBUtils;
import com.cuxiaoyi.db.Dao;
import com.cuxiaoyi.taobao.TaobaoProxy;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.request.MarketingPromotionDeleteRequest;
import com.taobao.api.response.MarketingPromotionDeleteResponse;

public class DeletePromotionAction extends ActionSupport implements Constants{

	static Logger _log = Logger.getLogger(DeletePromotionAction.class.getName());
	
	private MarketingPromotionDeleteRequest promotionDeleteRequest;
	private MarketingPromotionDeleteResponse promotionDeleteResponse;
	
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String sessionId = (String)session.get(TOP_SESSION);
		_log.info("deleting promotion: " + promotionDeleteRequest.getPromotionId());
		promotionDeleteResponse = TaobaoProxy.getClient().execute(promotionDeleteRequest, sessionId);
		if (promotionDeleteResponse.isSuccess())
		{
			Connection conn = null;
			try
			{
				conn = DBUtils.getConnection();
				Dao.INSTANCE.deletePromotion(promotionDeleteRequest.getPromotionId(), conn);
			}
			catch (Exception e)
			{
				_log.log(Level.SEVERE, "", e);
			}
			finally
			{
				DBUtils.close(conn);
			}
			return SUCCESS;
		}
		else
		{
			error(promotionDeleteResponse);
		}
		return SUCCESS;
	}

	public void setPromotionDeleteRequest(MarketingPromotionDeleteRequest promotionDeleteRequest) {
		this.promotionDeleteRequest = promotionDeleteRequest;
	}

	public MarketingPromotionDeleteRequest getPromotionDeleteRequest() {
		return promotionDeleteRequest;
	}

	public void setPromotionDeleteResponse(MarketingPromotionDeleteResponse promotionDeleteResponse) {
		this.promotionDeleteResponse = promotionDeleteResponse;
	}

	public MarketingPromotionDeleteResponse getPromotionDeleteResponse() {
		return promotionDeleteResponse;
	}
	
	private void error(TaobaoResponse rsp)
	{
		_log.info(rsp.getErrorCode() + " - " + rsp.getMsg() + " - " + rsp.getSubCode() + " - " + rsp.getSubMsg());
	}
}
